#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <dirent.h>
#include <fcntl.h>

struct message
{
    char cmd[10];
    char filename[20];
};

void list_file(struct message , char *, struct sockaddr_in);
void get_file(struct message , char *, char *, struct sockaddr_in);
void put_file(struct message , char *, char *, struct sockaddr_in);

int main(int argc, const char *argv[])
{
    struct sockaddr_in server_addr;
    struct message msg;
    char buf[100];
    char *arg[10];
    int i = 0;

    if (argc < 3)
    {
        fprintf(stderr, "usages: %s ip port\n", argv[0]);
        exit(-1);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(argv[2]));
    server_addr.sin_addr.s_addr = inet_addr(argv[1]);

    while (1)
    {
        memset(buf, 0, sizeof(buf));
        memset(&msg, 0, sizeof(msg));
        i = 0;

        printf("##################################\n");
        printf("function:\n");
        printf("Input \"list\" can list the file server's files\n");
        printf("Input \"get filename\" can download file from file server\n");
        printf("Input \"put filename\" can upload file to file server\n");
        printf("Input \"quit\" can quit the client\n");
        printf("##################################\n");
        
        printf(">");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = 0;

        if (strncmp(buf, "quit", 4) == 0)
            break;
       
        arg[i] = strtok(buf, " ");
        while(arg[i] != NULL)
            arg[++i] = strtok(NULL, " ");

        if (strcmp(arg[0], "list") == 0)
            list_file(msg, arg[0], server_addr);
        else if (strcmp(arg[0], "get") == 0)
            get_file(msg, arg[0], arg[1], server_addr);
        else if (strcmp(arg[0], "put") == 0)
            put_file(msg, arg[0], arg[1], server_addr);
    }

    return 0;
}

void list_file(struct message msg, char *cmd, struct sockaddr_in server_addr)
{
    char buf[100];
    int server_fd;

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("failed to create socket");
        exit(-1);
    }

    if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("failed to connect");
        exit(-1);
    }
    
    strcpy(msg.cmd, cmd);
    send(server_fd, &msg, sizeof(msg), 0);

    usleep(500);

    memset(buf, 0, sizeof(buf));
    recv(server_fd, buf, sizeof(buf), 0);
    printf("%s\n", buf);

    close(server_fd);

    return ;
}

void get_file(struct message msg, char *cmd, char *filename, struct sockaddr_in server_addr)
{
    int fd;
    int server_fd;
    char buf[1024];
    int num = 0;

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("failed to create socket");
        exit(-1);
    }
    
    if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("failed to connect");
        exit(-1);
    }

    if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
    {
        perror("failed to create file");
        exit(-1);
    }

    strcpy(msg.cmd, cmd);
    strcpy(msg.filename, filename);
    send(server_fd, &msg, sizeof(msg), 0);

    memset(buf, 0, sizeof(buf));
    while ((num = recv(server_fd, buf, sizeof(buf), 0)) > 0)
    {
        usleep(500);
        write(fd, buf, num);
        memset(buf, 0, sizeof(buf));
    }

    printf("DownLoad OK!\n");
    close(fd);
    close(server_fd);

    return ;
}

void put_file(struct message msg, char *cmd, char *filename, struct sockaddr_in server_addr)
{
    int fd;
    int server_fd;
    int num = 0;
    char buf[1024];

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("failed to create socket");
        exit(-1);
    }

    if (connect(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("failed to connect");
        exit(-1);
    }
    
    strcpy(msg.cmd, cmd);
    strcpy(msg.filename, filename);
    send(server_fd, &msg, sizeof(msg), 0);

    usleep(500);

    if ((fd = open(filename, O_RDONLY)) < 0)
    {
        perror("failed to open file");
        exit(-1);
    }

    memset(buf, 0, sizeof(buf));
    while ((num = read(fd, buf, sizeof(buf))) > 0)
        send(server_fd, buf, num, 0);

    printf("UpLoad OK!\n");
    close(fd);
    close(server_fd);

    return ;
}
